﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp5
{
    public partial class StudentForm : Form
    {
        public static string selectStr;
        public StudentForm()
        {
            InitializeComponent();
        }

        private void StudentForm_Load(object sender, EventArgs e)
        {
            dataGridView1.DataSource = WJNDBEntities.dbhelp.Student.ToList();
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        }

        private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {

        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            Students.Add f = new Students.Add();
            f.ShowDialog();
            dataGridView1.DataSource = WJNDBEntities.dbhelp.Student.ToList();
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            int i = dataGridView1.CurrentCell.ColumnIndex;
            int j = dataGridView1.CurrentCell.RowIndex;
            string sid = Convert.ToString(dataGridView1[0, j].Value).Trim();
            DialogResult MsgBoxResult;//设置对话框的返回值
            MsgBoxResult = MessageBox.Show("是否要删除选定行:学号为——" + sid + " 的第"+ (j+1)+"行内容？",
            "提示",//对话框的标题
            MessageBoxButtons.YesNo,//定义对话框的按钮，这里定义了YSE和NO两个按钮
            MessageBoxIcon.Question,//定义对话框内的图表式样，这里是一个黄色三角型内加一个感叹号
            MessageBoxDefaultButton.Button2);
            if (MsgBoxResult == DialogResult.Yes)
            {
                Student s = WJNDBEntities.dbhelp.Student.Where(o => o.SNo == sid).FirstOrDefault();
                try
                {
                    WJNDBEntities.dbhelp.Student.Remove(s);
                    WJNDBEntities.dbhelp.SaveChanges();
                    MessageBox.Show("删除成功！");
                }
                catch
                {
                    MessageBox.Show("删除失败！");
                }
            }
            dataGridView1.DataSource = WJNDBEntities.dbhelp.Student.ToList();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            selectStr = Convert.ToString(dataGridView1.CurrentRow.Cells["sNoDataGridViewTextBoxColumn"].Value);
            Students.Change f = new Students.Change();
            f.StartPosition = FormStartPosition.CenterParent;

            f.ShowDialog();
        }
    }
}
